package online.cmagic.structureType.flyweight;

/**
 * 享元模式
 *
 * 享元模式主要是减少创建对象的数量，以减少内存占用和提高性能。尝试重用现有的同类对象，如果未找到匹配的对象，则创建新的对象。
 * 优点：减少对象的创建，降低内存。
 * 缺点：提高了系统的复杂度，需要分理出内部和外部状态。
 * @author wsc
 * @date: 2020年04月15日 15:16
 * @since JDK 1.8
 */
public class TestClient {
    public static void main(String[] args) {
        int n = 10;
        FlyWeightFactory flyWeightFactory = new FlyWeightFactory();
        FlyWeight flyWeight1 = flyWeightFactory.getFactory().get("X");
        FlyWeight flyWeight2 = flyWeightFactory.getFactory().get("X");
        FlyWeight flyWeight3 = flyWeightFactory.getFactory().get("X");
        UnsharedConcreteFlyWeight unsharedConcreteFlyWeight = new UnsharedConcreteFlyWeight();
        flyWeight1.operation(--n);
        flyWeight2.operation(--n);
        flyWeight3.operation(--n);
        unsharedConcreteFlyWeight.operation(--n);
    }
}
